/**
 * Created by Administrator on 2017/9/2.
 */
var i,
    input=$("#query_input"),
    input_last=input.value,
    result_ul=$("#query_result"),
    lrcUl=$("#lrcUl"),
    audio=$("#audio"),
    music_auto_continue_on=true,
    music_animate_on=true;
$(function ()
{
    init_audio();
    query_input_events();
    result_click();
    music_auto_continue();
});
//初始化播放器
function init_audio() {
    audio.volume=0.1;
    music_auto_continue_on&&(audio.currentTime=localStorage.getItem("data_playing"));
    music_animate_on&&animate();
}
//输入框事件
function query_input_events()
{
    input.addEventListener("input",function ()
    {
        input.value = input.value.replace(/\s+/g, ' ');
        if(input.value!=""&&input.value!=input_last&&input.value.trim()!=input_last.trim())
        {
            input_last=input.value;
            query(input.value,"callback")
        }
        else if(input.value.trim()=="")
        {
           result_ul.innerHTML=""
        }
    })
}
//搜索回调函数
function callback(json)
{
    result_ul.innerHTML="";
    var song_array=json.song;
    if(song_array.length!=0)
    {
        log(json);
        for(i=0;i<song_array.length;i++)
        {
            var li=_.createElement("LI");
            li.setAttribute("id",song_array[i].songid);
            li.innerHTML=song_array[i].songname+" "+song_array[i].artistname;
            result_ul.appendChild(li)
        }
    }
}
//返回结果点击事件
function result_click()
{
    result_ul.addEventListener("click",function (e)
    {
        lrcUl.innerHTML=" ";
        if(e.target&&e.target.nodeName=="LI")
        {
            get_lrc(e.target.id,"show_lrc");
            audio_play(e.target.id,"music_play");
        }
    })
}
//歌词回调方法
function show_lrc(json) {
    var lrc=json.lrcContent;
    //替换换行符为"<br>"
    lrc=deal_line_break(lrc);
    //歌词分割为数组
    lrc=lrc.split("<br>");
    //处理歌词为可用格式
    lrc=baiduLrc_to_lrc2(lrc)[0];

    //遍历歌词数组 创建元素
    for(i=0;i<lrc.length;i++){
        var li=_.createElement("LI");
        var span=_.createElement("span");
        span.innerHTML=lrc[i][1];
        li.appendChild(span);
        lrc[i].splice(2,1,li);
        lrcUl.appendChild(li);
    }
    //ul样式
    //alert(lrcUl.offsetHeight/2);
    lrcUl.style.top=lrcUl.offsetHeight/2+"px";
    //歌词滚动
    audio.addEventListener("timeupdate",function () {
        var k=0;
        var now=0;
        for(i=0;i<lrc.length;i++){
            lrc[i][2].style.color="";
            lrc[i][2].childNodes[0].style.boxShadow="";
            if (audio.currentTime>lrc[i][0])
            {
                now=i;
            }
        }
        if(lrc[now][2].style.color!=="#fff")
        {
            lrcUl.style.top=210-30*now+"px";
            lrc[now][2].style.color="#fff";
            lrc[now][2].childNodes[0].style.boxShadow="0 1px 0 #fff";
        }

    });
}
function music_play(json)
{
    log(json);
    audio.src=json.bitrate.file_link;
    $("#pic").src=json.songinfo.pic_radio;
    $("#bgp").style.backgroundImage="url("+json.songinfo.pic_radio+")";
}
//用<br>代替换行符
function deal_line_break(mobiles) {
    var i;
    var result = "";
    var c;
    for (i = 0; i < mobiles.length; i++) {
        c = mobiles.substr(i, 1);
        if (c == "\n")
            result = result + "<br>";
        else if (c != "\r")
            result = result + c;
    }
    c=result;
    return c;
}
function music_auto_continue() {
    if(music_auto_continue_on){
        setInterval(function () {
            var playing=audio.currentTime;
            localStorage.setItem("data_playing",playing);
        },200);
    }
}
//动画绘制
function animate() {
    var canvas=$("#canvas");
    var catch_canvas=_.createElement("canvas");
    var real_ctx=canvas.getContext("2d");
    var ctx=catch_canvas.getContext("2d");
    draw();
    var x=0,y=0;

    function draw() {
        ctx.clearRect(0,0,canvas.width,canvas.height);
        //ctx.fillRect(x++,y++,200,200);
        real_ctx.clearRect(0,0,canvas.width,canvas.height);
        real_ctx.drawImage(catch_canvas,0,0);
        music_animate_on&&requestAnimationFrame(draw);
    }
    //自适应画布
    function canvas_autoWH() {
        var w=canvas.offsetWidth,
            h=canvas.offsetHeight;
        canvas.width=w;
        canvas.height=h;
        catch_canvas.width=w;
        catch_canvas.height=h;
    }canvas_autoWH();
    win.addEventListener("resize",canvas_autoWH);

}
//处理百度歌词
function baiduLrc_to_lrc2(arr)
{
    var lrc=[];
    var info=[];
    for(i=0;i<arr.length;i++)
    {
        var str = arr[i];
        var regex=  /\[(.+?)\]/g;
        var index=str.match(regex);
        if(index)
        {
            for(var j=0;j<index.length;j++)
            {
                //处理歌词字符串
                var lrc_str;
                lrc_str=arr[i].split("]");
                lrc_str=lrc_str[lrc_str.length-1];
                (lrc_str=="")&&(lrc_str="  ");
                //处理时间
                var time=index[j].substr(1,index[j].length-2);
                time=time.split(":");
                if(!isNaN(time[1])&&!isNaN(time[0]))
                {
                    time=time[0]*60+Number(time[1]);
                    lrc.push([time,lrc_str])
                }else
                {
                    info.push(time)
                }
            }
        }
    }
    //歌词排序
    var len=lrc.length;
    for(i=0;i<len;i++)
    {
        for(var k=0;k<len-i-1;k++)
        {
            if(lrc[k][0]>lrc[k+1][0]){
                var temp=lrc[k+1];
                lrc[k+1]=lrc[k];
                lrc[k]=temp
            }
        }
    }
    return [lrc,info];
}
get_lrc("242078437","show_lrc");
audio_play("242078437","music_play");
